package com.unohacha.qqj.fzdr.personal.view.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.common.utils.TemporaryDataUtil;
import com.unohacha.qqj.fzdr.imp.MyApplication;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 修改密码
 */
public class ModifyPasswordActivity extends BaseActivity {

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;

    @BindView(R.id.et_old_password)
    EditText etOldPassword;

    @BindView(R.id.et_password)
    EditText etPassword;

    @BindView(R.id.et_passwordr)
    EditText etPasswordR;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_password;
    }

    @OnClick(R.id.tv_forget_password)
    void onForgetPasswordClick() {
        jumpToOtherActivity(this, ForgetPasswordActivity.class);
    }

    @OnClick(R.id.common_back)
    void onBackClick() {
        finish();
    }

    // 修改密码
    @OnClick(R.id.btn_change_password)
    void onBtnChangePasswordClick() {
        String oldPassword = etOldPassword.getText().toString().trim();
        String newPassword = etPassword.getText().toString().trim();
        String newRPassword = etPasswordR.getText().toString().trim();
        showLoading();
        if (!TextUtils.isEmpty(oldPassword) && !TextUtils.isEmpty(newPassword) && !TextUtils.isEmpty(newRPassword)) {
            Call<HttpResult<String>> call = RetrofitManager.getInstance().revisePwd(
                    Prefer.getInstance().getToken(),
                    TemporaryDataUtil.getUserInfoBean().getTelephone(), oldPassword, newPassword, newRPassword);
            call.enqueue(new Callback<HttpResult<String>>() {
                @Override
                public void onResponse(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                    String data = response.body().getData();
                    if (data != null) {
                        // 修改成功
                        Toast.makeText(getApplicationContext(), "修改密码成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        String msg = response.body().getMessage();
                        if (msg != null) {
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            // 退出重新登录
                            MyApplication.getInstance().gotoLoginActivity();
                            finish();
                        } else {
                            Toast.makeText(getApplicationContext(), "未知错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                    hideLoading();
                }

                @Override
                public void onFailure(Call<HttpResult<String>> call, Throwable t) {
                    Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                    hideLoading();
                }
            });
        }
    }

    @Override
    protected void initView() {
//        super.initView();
//        commonTitle.setText("");
        commonTitle.setVisibility(View.INVISIBLE);
        commonRightImage.setVisibility(View.INVISIBLE);

    }
}
